//package com.sidianzhong.sdz.task;
//
//import com.alibaba.fastjson.JSONObject;
//import com.sidianzhong.sdz.model.*;
//import com.sidianzhong.sdz.service.*;
//import com.sidianzhong.sdz.utils.Constants;
//import com.sidianzhong.sdz.utils.ResultModel;
//import com.sidianzhong.sdz.utils.WalletAccount;
//import com.sidianzhong.sdz.utils.WalletProcess;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.models.auth.In;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.TaskScheduler;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.*;
//import java.util.function.Consumer;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
//@Component
//@Service
//@RestController
//@EnableScheduling
//public class GeneTask {
//
//    //设定线程池子信息
//    @Bean
//    public TaskScheduler taskScheduler() {
//        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
//        taskScheduler.setPoolSize(50);
//        return taskScheduler;
//    }
//
//    @Autowired
//    MyNodeService myNodeService;
//    @Autowired
//    NodeIncomeService nodeIncomeService;
//    @Autowired
//    TouchNumService touchNumService;
//    @Autowired
//    MyOrderService myOrderService;
//    @Autowired
//    ManageIncomeService manageIncomeService;
//    @Autowired
//    ManageInvestService manageInvestService;
//    @Autowired
//    UserService userService;
//    @Autowired
//    CoinInfoService coinInfoService;
//    @Autowired
//    ConstantService constantService;
//    @Autowired
//    MyMillService myMillService;
//    @Autowired
//    MillIncomeService millIncomeService;
//    @Autowired
//    MillRatioService millRatioService;
//    @Autowired
//    UserWalletService userWalletService;
//    @Autowired
//    UserChangeService userChangeService;
//    @Autowired
//    MillIncomeCollectionService millIncomeCollectionService;
//
//    /**
//     * 每日   理财存单返利
//     */
//    @Scheduled(cron = "0 0 0 * * ?")
//    public void manageInvest() {
//        //获取到所有用户
//        User user = new User();
//        List<User> userList = userService.getList(1, 100000000, "id", "asc", user);
//
//        //获取到所有的coin列表
//        CoinInfo coinInfo = new CoinInfo();
//        List<CoinInfo> coinInfoList = coinInfoService.getList(1, 100000000, "coin_id", "asc", coinInfo);
//
//        //投资利率
//        Double investRate = Double.valueOf(constantService.selectValueByKey(Constants.MANAGE_RATIO));
//
//        Date date = new Date();
//
//        //MSB的币种
//        Integer returnCoinId = 10000006;
//
//        //获取到MSB汇率
//
//        CoinInfo coinInfo2 = coinInfoService.get(returnCoinId);
//        Double msbCoinPriceRmb = coinInfo2.getCoinPriceRmb();
//
//        userList.stream().forEach(user1 -> {
//            //获取用户Id
//            Integer userId = user1.getId();
//            //这个用户需要返利的数量
//            coinInfoList.stream().forEach(coinInfo1 -> {
//                //循环获取币种信息
//                Integer coinId = coinInfo1.getCoinId();
//                //获取到这个用户 这个币种 全部理财转出的数量
//                Double outInvest = manageInvestService.getInvest(userId, coinId, 1);
//                //获取到这个用户 这个币种  两天前全部的转入数量
//                Double inInvest = manageInvestService.getTwoDayInvest(userId, coinId, 0);
//                //判断下转出数量是否大于或者等于转入数量
//                Double investNumber = inInvest - outInvest;
//                //如果大于0  说明理财存单还有余额
//                if (investNumber > 0) {
//                    //计算这个币种换算为人民币汇率
//                    Double coinPriceRmb = coinInfo1.getCoinPriceRmb();
//                    //计算理财返利金额(人民币)
//                    Double investAmount = investNumber * investRate * coinPriceRmb;
//
//                    //存入数据库
//                    ManageIncome manageIncome = new ManageIncome();
//                    manageIncome.setCoinId(coinId);
//                    manageIncome.setUserId(userId);
//                    manageIncome.setStatus(0);
//                    manageIncome.setValue(investAmount / msbCoinPriceRmb);
//                    manageIncome.setCreateTime(date);
//                    manageIncome.setLastUpdateTime(date);
//
//                    manageIncomeService.create(manageIncome);
//
//
//                    //获取到这个用户的MSB余额
//                    UserWallet userWallet = userWalletService.getByUserId(userId, returnCoinId);
//                    Double amount = userWallet.getAmount();
//                    userWallet.setAmount(amount + investAmount);
//                    userWalletService.update(userWallet);
//                } else {
//                    //跳过计算
//                }
//            });
//        });
//    }
//
//    /**
//     * 每日   矿机结算归集
//     */
//    @Scheduled(cron = "0 0 0 * * ?")
//    public void millInvestToList() {
//        //获取到当前所有矿机列表
//        MyMill myMill = new MyMill();
//        List<MyMill> myMillList = myMillService.getList(1, 100000000, "id", "asc", myMill);
//
//        Date date = new Date();
//        myMillList.stream().forEach(myMill1 -> {
//            Integer userId = myMill1.getUserId();
//            //求出总共的收益
//            Double value = millIncomeService.getValue(userId, myMill.getId(), 0);
//            MillIncomeCollection millIncomeCollection = new MillIncomeCollection();
//            millIncomeCollection.setOrderId(myMill1.getCommidityId());
//            millIncomeCollection.setUserId(myMill1.getUserId());
//            millIncomeCollection.setValue(value);
//            millIncomeCollection.setStatus(0);
//            millIncomeCollection.setCreateTime(date);
//            millIncomeCollection.setLastUpdateTime(date);
//            millIncomeCollectionService.create(millIncomeCollection);
//        });
//
//        //清数据
//        millIncomeService.truncateTable();
//    }
//
//    /**
//     * 15分钟  矿机挖矿结算
//     */
//    @Scheduled(cron = "0 */15 * * * ?")
//    public void millInvest() {
//        //获取到当前的返利矿机数量区间
//        MillRatio millRatio = new MillRatio();
//        List<MillRatio> millRatioList = millRatioService.getList(1, 100000000, "id", "asc", millRatio);
//        //获取到总共的币数量
//        Double totalAmount = Double.valueOf(constantService.selectValueByKey(Constants.MSB_NUM));
//        //获取到挖矿的总年数
//        Double totalYear = Double.valueOf(constantService.selectValueByKey(Constants.MSB_MILL_YEAR));
//        //计算出每次返利的总数量
//        Double nowIncomeAmount = totalAmount / totalYear / 365 / 24 / 4;
//        //获取到当前所有矿机列表
//        MyMill myMill = new MyMill();
//        List<MyMill> myMillList = myMillService.getList(1, 100000000, "id", "asc", myMill);
//        //获取到所有矿机的数量
//        Integer size = myMillList.size() + Integer.valueOf(constantService.selectValueByKey(Constants.MILL_BASE));
//        //如果没有矿机 就停止
//        if (size <= 0) return;
//        //循环获取矿机数量
//        for (MillRatio ratio : millRatioList) {
//            Integer startNum = ratio.getStartNum();
//            Integer endNum = ratio.getEndNum();
//            if (size > startNum && size <= endNum) {
//                size = endNum;
//                break;
//            }
//        }
//        //计算出每台机器获取到的利益
//        Double millIncomeValue = nowIncomeAmount / size;
//        Date date = new Date();
//        //MSB的币种
//        Integer coinId = 10000006;
//        //开始返利
//        myMillList.stream().forEach(myMill12 -> {
//            Integer userId = myMill12.getUserId();
//            MillIncome millIncome = new MillIncome();
//            millIncome.setOrderId(myMill12.getId());
//            millIncome.setUserId(userId);
//            millIncome.setCoinId(coinId);
//            millIncome.setValue(millIncomeValue);
//            millIncome.setStatus(0);
//            millIncome.setCreateTime(date);
//            millIncome.setLastUpdateTime(date);
//            millIncomeService.create(millIncome);
//
//            //获取到这个用户的MSB余额
//            UserWallet userWallet = userWalletService.getByUserId(userId, coinId);
//            Double amount = userWallet.getAmount();
//            userWallet.setAmount(amount + millIncomeValue);
//            userWalletService.update(userWallet);
//        });
//    }
//
//    /**
//     * 每周周一   碰对结算 / 节点返利
//     * 注 ： 分红 Status 2
//     */
//    @Scheduled(cron = "0 0 0 ? * 2")
//    public void node() {
//        touchInvest();
//
////        nodeInvest();
//    }
//
//    private void touchInvest() {
//        //获取到所有用户
//        MyNode myNode = new MyNode();
//        List<MyNode> myNodeList = myNodeService.getList(1, 100000000, "id", "asc", myNode);
//
//        Double totalNumber = myOrderService.getLastAmount(1);
//
//        List<JSONObject> listwww = new ArrayList<>();
//
//        //获取到碰对规则
//        Integer depthNum = Integer.valueOf(constantService.selectValueByKey(Constants.DEPTH_NUM));
//        Integer widthNum = Integer.valueOf(constantService.selectValueByKey(Constants.WIDTH_NUM));
//        Double giftRate = Double.valueOf(constantService.selectValueByKey(Constants.TOUCH_RATIO));
//
//        //返利的CoinId
//        Integer coinId = 10000004;
//
//        Date date = new Date();
//
//        //本次可以碰对的比例
//        Map<Integer, Integer> touchRate = new HashMap<>();
//
//        //计算碰对次数
//        myNodeList.stream().forEach(myNode1 -> {
//            Integer userId = myNode1.getUserId();
//
//            //获取到这个用户宽度和高度
//            List<MyNode> widthIList = myNodeService.getWidthIList(userId);
//            if (widthIList == null || widthIList.size() < widthNum) {
//                //宽度小于4 就停止
//                return;
//            }
//            List<MyNode> depthIList = myNodeService.getDepthIList(userId);
//            if (depthIList == null || depthIList.size() < depthNum) {
//                //深度小于8 就停止
//                return;
//            }
//            //获取到这个用户所有碰对记录
//            List<TouchNum> touchNumList = touchNumService.getDataByUserId(userId);
//
//            //计算出当前实际可碰对的数量
//            Integer width = widthIList.size() - widthNum * touchNumList.size();
//            Integer depth = depthIList.size() - depthNum * touchNumList.size();
//            if (width < widthNum && depth < depthNum) {
//                //如果实际可碰对的数量都不满足  4 8 就return
//                return;
//            }
//            Integer widthCount = width / widthNum;
//            Integer depthCount = depth / depthNum;
//
//            if (widthCount < 1 || depthCount < 1) {
//                return;
//            }
//            Integer touchNum = (widthCount <= depthCount) ? widthCount : depthCount;
//
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("userId", userId);
//            jsonObject.put("num", touchNum);
//            listwww.add(jsonObject);
//
//
//            //开始碰对
//            TouchNum touchNum1 = new TouchNum();
//            touchNum1.setNum(touchNum);
//            touchNum1.setUserId(userId);
//            touchNum1.setCreateTime(date);
//            touchNum1.setLastUpdateTime(date);
//            touchNumService.create(touchNum1);
//        });
//        Integer num = listwww.stream().mapToInt(ww -> {
//            return ww.getInteger("num");
//        }).sum();
//
//        listwww.stream().forEach(sss -> {
//            Integer userId = sss.getInteger("userId");
//            Integer num1 = sss.getInteger("num");
//            double v = totalNumber * giftRate / num * num1;
//
//            NodeIncome nodeIncome = new NodeIncome();
//            nodeIncome.setCoinId(coinId);
//            nodeIncome.setUserId(userId);
//            nodeIncome.setValue(v);
//            nodeIncome.setCreateTime(date);
//            nodeIncome.setLastUpdateTime(date);
//            nodeIncome.setStatus(2);
//            nodeIncomeService.create(nodeIncome);
//
//            //获取到这个用户的USDT余额
//            UserWallet userWallet = userWalletService.getByUserId(userId, coinId);
//            Double amount = userWallet.getAmount();
//            userWallet.setAmount(amount + v);
//            userWalletService.update(userWallet);
//        });
//    }
//
////    private void nodeInvest() {
////        //获取到所有节点
////        MyNode myNode = new MyNode();
////        Integer coinId = 10000004;
////        Date date = new Date();
////        Double giftRate = Double.valueOf(constantService.selectValueByKey(Constants.NODE_INVEST));
////        List<MyNode> myNodeList = myNodeService.getList(1, 100000000, "id", "asc", myNode);
////
////        //计算每一个节点的返利
////        for (MyNode myNode1 : myNodeList) {
////            Integer userId = myNode1.getUserId();
////
////            //获取到宽度列表
////            List<MyNode> widthList = myNodeService.getNextList(myNode1.getWidthId());
////
////            //获取到直推列表
////            MyNode myNode2 = new MyNode();
////            myNode2.setInviter(myNode1.getUserId());
////            List<MyNode> myInviterList = myNodeService.getList(1, 100000000, "id", "asc", myNode2);
//////            List<MyNode> width = new ArrayList<>();
//////            HashSet hs1 = new HashSet(widthList);
//////            HashSet hs2 = new HashSet(myInviterList);
//////            hs1.removeAll(hs2);
//////            width.addAll(hs1);
////            //获取所有直线宽度10%
////            List<MyNode> collect = widthList.stream().filter(node -> node.getCreateTime().getTime() < date.getTime()).filter(nodes -> nodes.getCreateTime().getTime() > date.getTime() - 86400 * 7 * 1000).collect(Collectors.toList());
////            collect.stream().forEach(cl -> {
////                MyOrder byUsedId = myOrderService.getByUsedId(cl.getUserId(), 1);
////                Double investAmount = byUsedId.getPrice() * giftRate;
////                NodeIncome nodeIncome = new NodeIncome();
////                nodeIncome.setCoinId(coinId);
////                nodeIncome.setUserId(userId);
////                nodeIncome.setInviterId(cl.getUserId());
////                nodeIncome.setValue(investAmount);
////                nodeIncome.setCreateTime(date);
////                nodeIncome.setLastUpdateTime(date);
////                nodeIncome.setStatus(1);
////                nodeIncomeService.create(nodeIncome);
////
////                //获取到这个用户的USDT余额
////                UserWallet userWallet = userWalletService.getByUserId(userId, coinId);
////                Double amount = userWallet.getAmount();
////                userWallet.setAmount(amount + investAmount);
////                userWalletService.update(userWallet);
////            });
////
////            //获取所有直推宽度10%
////            List<MyNode> collect1 = myInviterList.stream().filter(node -> node.getCreateTime().getTime() < date.getTime()).filter(nodes -> nodes.getCreateTime().getTime() > date.getTime() - 86400 * 7 * 1000).collect(Collectors.toList());
////            collect1.stream().forEach(cl1 -> {
////                List<MyNode> widthList1 = myNodeService.getNextList(cl1.getWidthId());
////                widthList1.stream().filter(node -> node.getCreateTime().getTime() < date.getTime()).filter(nodes -> nodes.getCreateTime().getTime() > date.getTime() - 86400 * 7 * 1000).forEach(nodess -> {
////                    MyOrder byUsedId = myOrderService.getByUsedId(nodess.getUserId(), 1);
////                    Double investAmount = byUsedId.getPrice() * giftRate;
////                    NodeIncome nodeIncome = new NodeIncome();
////                    nodeIncome.setCoinId(coinId);
////                    nodeIncome.setUserId(userId);
////                    nodeIncome.setInviterId(nodess.getUserId());
////                    nodeIncome.setValue(investAmount);
////                    nodeIncome.setCreateTime(date);
////                    nodeIncome.setLastUpdateTime(date);
////                    nodeIncome.setStatus(1);
////                    nodeIncomeService.create(nodeIncome);
////
////                    //获取到这个用户的USDT余额
////                    UserWallet userWallet = userWalletService.getByUserId(userId, coinId);
////                    Double amount = userWallet.getAmount();
////                    userWallet.setAmount(amount + investAmount);
////                    userWalletService.update(userWallet);
////                });
////            });
////
////        }
////    }
//
//    /**
//     * 一分钟   同步交易记录
//     */
//    @Scheduled(cron = "0 */1 * * * ?")
//    public void doChange() {
//        UserChange userChange = new UserChange();
//        //查询转账中
//        userChange.setStatus(0);
//        List<UserChange> userChangeList = userChangeService.getList(1, 100000000, "id", "asc", userChange);
//        userChangeList.stream().forEach(userChange1 -> {
//            String tmpHash = userChange1.getDrawHash();
//            //判断hash是否交易成功
//            WalletProcess walletProcess = new WalletProcess();
//            WalletAccount walletAccount;
//            switch (userChange1.getCoinId()) {
//                case 10000001:
//                    walletAccount = walletProcess.DoCheckTxBTC(tmpHash);
//                    break;
//                case 10000002:
//                    walletAccount = walletProcess.DoCheckTxETH(tmpHash);
//                    break;
//                case 10000003:
//                    walletAccount = walletProcess.DoCheckTxBUSDT(tmpHash);
//                    break;
//                case 10000004:
//                    walletAccount = walletProcess.DoCheckTxEUSDT(tmpHash);
//                    break;
//                case 10000005:
//                    walletAccount = walletProcess.DoCheckTxDWTT(tmpHash);
//                    break;
//                case 10000006:
//                    walletAccount = walletProcess.DoCheckTxMSB(tmpHash);
//                    break;
//                default:
//                    return;
//            }
//
//            if (walletAccount.getResult()) {
//                int hashStatus = walletAccount.getHashStatus();
//                switch (hashStatus) {
//                    //成功
//                    case 0:
//                        //支付成功
//                        userChange1.setStatus(1);
//                        this.userChangeService.update(userChange1);
//                    case 1:
//                        return;
//                    case 2:
//                        //支付失败
//                        userChange1.setStatus(2);
//                        this.userChangeService.update(userChange1);
//
//                        //资金回退
//                        UserWallet userWallet = userWalletService.getByUserId(userChange1.getUserId(), userChange1.getCoinId());
//                        Double amount = userWallet.getAmount();
//                        userWallet.setAmount(amount + userChange1.getAmount() + userChange1.getFee());
//                        userWalletService.update(userWallet);
//                        break;
//                    default:
//                        break;
//                }
//            }
//        });
//    }
//
//
//    @ApiOperation(value = "结算")
//    @RequestMapping(value = "/doTest",
//            method = RequestMethod.POST,
//            produces = "application/json;charset=UTF-8")
//    @ResponseBody
//    public Object node1(
//    ) {
//        millInvest();
//        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
//    }
//
//    @ApiOperation(value = "结算")
//    @RequestMapping(value = "/doTest1",
//            method = RequestMethod.POST,
//            produces = "application/json;charset=UTF-8")
//    @ResponseBody
//    public Object node2(
//    ) {
//        millInvestToList();
//        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
//    }
//}
